package ar.com.drconta.ganancias.editors.example;
 
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.IFormPage;
 
 
public class MyEditor extends FormEditor{
   public static final String ID = "ar.com.drconta.ganancias.editors.example.MyEditor";
 
   public MyEditor() {
      super();
   }
 
   @Override
   protected void addPages(){
      try{
         addPage(new MyEditorPage(this, "PageId-1", "MyEditorPage-1"));
         addPage(new MyEditorPage(this, "PageId-2", "MyEditorPage-2"));
      }catch (PartInitException e){
         e.printStackTrace();
      }
   }
 
   @Override
   public void doSave(IProgressMonitor monitor){
   }
 
   @Override
   public void doSaveAs(){
   }
 
   @Override
   public boolean isSaveAsAllowed(){
      return false;
   }
 
   public static IEditorPart openEditor(final Object myDataObject) {
      final IWorkbenchPage wbp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
      final ObjectHolder editorHolder = new ObjectHolder();
      Display.getCurrent().asyncExec(new Runnable() {
         public void run() {
        	 System.out.println(myDataObject.getClass());
            final MyEditorInput input = new MyEditorInput(myDataObject);
            IEditorPart editor;
            try{
               editor = wbp.openEditor(input, ID);
            }catch (PartInitException e){
               // TODO Auto-generated catch block
               e.printStackTrace();
               editor = null;
            }
            editorHolder.setObj(editor);
         }
      });
      IEditorPart editorPart = (IEditorPart) editorHolder.getObj();
      return editorPart;
   }
 
}
 
 
class MyEditorInput extends PlatformObject implements IEditorInput {
   Object obj;
 
   public MyEditorInput(Object obj) {
      this.obj = obj;
   }
 
   public boolean exists(){
      return false;
   }
 
   public ImageDescriptor getImageDescriptor(){
      return null;
   }
 
   public String getName(){
      return obj.toString();
   }
 
   public IPersistableElement getPersistable(){
      return null;
   }
 
   public String getToolTipText(){
      return obj.toString();
   }
}
 
 
class ObjectHolder {
   private Object obj = null;
 
   public Object getObj(){
      return obj;
   }
 
   public void setObj(Object obj){
      this.obj = obj;
   }
}